<template>
  <div class="container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="220px">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="userInfo.username" style="width: 380px;" type="text" size="mini" />
          </el-form-item>
          <el-form-item label="工号" prop="workNumber">
            <el-input v-model="userInfo.workNumber" style="width: 380px;" disabled type="text" size="mini" />
          </el-form-item>
          <!-- 编辑时 手机号不可修改 -->
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="userInfo.mobile" style="width: 380px;" type="text" size="mini" :disabled="!!$route.params.id" />
          </el-form-item>
          <el-form-item label="部门" prop="departmentId">
            <!-- 放置及联部门组件 v-model双向绑定选择的部门id -->
            <select-tree v-model="userInfo.departmentId" />
          </el-form-item>
          <el-form-item label="聘用形式" prop="formOfEmploymente">
            <el-select v-model="userInfo.formOfEmployment" size="mini" style="width: 380px;">
              <el-option label="正式" :value="1" />
              <el-option label="非正式" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker v-model="userInfo.timeOfEntry" type="date" size="mini" style="width: 380px;" value-format="yyyy-MM-dd" />
          </el-form-item>
          <el-form-item label="转正时间" prop="correctionTime">
            <el-date-picker v-model="userInfo.correctionTime" type="date" size="mini" style="width: 380px;" />
          </el-form-item>
          <el-form-item label="员工头像">
            <!-- 上传员工头像 使用组件封装-->
            <image-upload v-model="userInfo.staffPhoto" />
          </el-form-item>
          <el-form-item>
            <!-- 表单通过 提交 -->
            <el-button type="primary" size="mini" @click="saveData">保存更新</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { addEmployee, getEmployeeDetail, updateEmployee } from '@/api/employee'
import selectTree from './selectTree.vue'
import imageUpload from './imageUpload.vue'
export default {
  components: { selectTree, imageUpload },
  data() {
    return {
      userInfo: {
        username: '', // 员工姓名
        workNumber: '', // 员工工号
        mobile: '', // 员工手机号
        departmentId: null, // 员工部门id
        formOfEmployment: null, // 聘用形式
        timeOfEntry: '', // 入职日期
        correctionTime: '', // 转正日期
        staffPhoto: '' // 员工头像
      },
      rules: {
        // 添加表单检验规则
        username: [{ required: true, message: '请填写员工姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名为1到4位' }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' },
          {
            // 添加自定义表单检验 转正时间不能小于入职时间
            validator: (rule, value, callback) => {
              // 已有入职时间的情况下
              if (this.userInfo.timeOfEntry) {
                // 取值判断
                if (new Date(this.userInfo.timeOfEntry) > new Date(value)) {
                  callback(new Error('转正时间不能小于入职时间'))
                  return
                }
              }
              callback()
            }
          }]
      }
    }
  },
  created() {
    // 判断是否有id 有就查询员工详情数据
    this.$route.params.id && this.getEmployeeDetail()
  },
  methods: {
    saveData() {
      // 表单校验通过
      this.$refs.userForm.validate(async isOK => {
        if (isOK) {
          // 区别编辑模式还是新增模式
          if (this.$route.params.id) {
            await updateEmployee(this.userInfo)
            this.$message.success('编辑员工信息成功')
          } else {
            // 调用接口
            await addEmployee(this.userInfo)
            // 提示信息
            this.$message.success('新增员工信息成功')
          // 返回员工列表页面
          }
          this.$router.push('/employee')
        }
      })
    },
    // 员工详情页判断是否有id，有id就查询详情数据
    async getEmployeeDetail() {
      this.userInfo = await getEmployeeDetail(this.$route.params.id)
    }
  }
}
</script>
<style scoped lang="scss">
.edit-form{
  color: #fff;
  padding: 20px;
}
</style>
